<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        $this->display();   //引模板
    }
    public function welcome()
    {
        if(S('welcomeData')){
         // var_dump(888);
         $data = S('welcomeData');     //使用缓存
     }else{
         // var_dump(777);
         $data = $this->conutData();
         S('welcomeData',$data ,3600);   //缓存，每个一小时更新一次数据
     }

     $this->assign('data',$data);
     $this->display();
    }

    public function conutData()
    {

        $str = date('y-m-d');   //得出今天日期
        // var_dump($str);

        $time = strtotime(date('y-m-d'));    //得出今天凌晨时间戳

        $yesterday = strtotime("-1 days",$time);  //得出昨天凌晨的时间戳

        $week = strtotime("-1 week",$time);  //得出一周前凌晨的时间戳

        $month = strtotime("-1 month",$time);   //得出一个月前凌晨的时间戳

        
        $data = array();
        
        $sql = "select count(id) cn from user ";
        $sql .= "UNION ALL select count(id) cn from user where create_time>'{$time}'";
        //统计今天注册的人数
        $sql .= "UNION ALL select count(id) cn from user where create_time>'{$yesterday}' and create_time<'{$time}'";
        //统计大于昨天凌晨并小于今天凌晨的昨天注册的人数
      
        $sql .= "UNION ALL select count(id) cn from user where create_time>'{$week}'";

        $sql .= "UNION ALL select count(id) cn from user where create_time>'{$month}'";

        $res = M()->query($sql);
        $data['总数'][] =  $res[0]['cn'];
        $data['今天'][] =  $res[1]['cn'];
        $data['昨天'][] =  $res[2]['cn'];
        $data['一周'][] =  $res[3]['cn'];
        $data['一月'][] =  $res[4]['cn'];

       //统计问题的总数
        $sql = "select count(id) cn from question ";
        $sql .= "UNION ALL select count(id) cn from question where create_time>'{$time}'";

        $sql .= "UNION ALL select count(id) cn from question where create_time>'{$yesterday}' and create_time<'{$time}'";

        $sql .= "UNION ALL select count(id) cn from question where create_time>'{$week}'";

        $sql .= "UNION ALL select count(id) cn from question where create_time>'{$month}'";

        $res = M()->query($sql);
        $data['总数'][] =  $res[0]['cn'];
        $data['今天'][] =  $res[1]['cn'];
        $data['昨天'][] =  $res[2]['cn'];
        $data['一周'][] =  $res[3]['cn'];
        $data['一月'][] =  $res[4]['cn'];


        //统计回复的总数
        $sql = "select count(id) cn from answer ";
        $sql .= "UNION ALL select count(id) cn from answer where create_time>'{$time}'";

        $sql .= "UNION ALL select count(id) cn from answer where create_time>'{$yesterday}' and create_time<'{$time}'";
   
        $sql .= "UNION ALL select count(id) cn from answer where create_time>'{$week}'";

        $sql .= "UNION ALL select count(id) cn from answer where create_time>'{$month}'";
        
        $res = M()->query($sql);
        $data['总数'][] =  $res[0]['cn'];
        $data['今天'][] =  $res[1]['cn'];
        $data['昨天'][] =  $res[2]['cn'];
        $data['一周'][] =  $res[3]['cn'];
        $data['一月'][] =  $res[4]['cn'];

        return $data;

        // $data['总数'][] = M()->query($sql)[0]['cn'];
        // $data['今天'][] = M()->query($sql)[1]['cn'];
        // $data['昨天'][] = M()->query($sql)[2]['cn'];
        // $data['本周'][] = M()->query($sql)[3]['cn'];
        // $data['本月'][] = M()->query($sql)[4]['cn'];


    }
    public function getData()
    {
        $arr = $data;

        echo json_encode($arr);
    }
}







    
